/* Raj Akula
   CS 362 
   Project One
   08-28-2009
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{

	//FILE POINTER TO READ FROM FILE
	FILE *file;
	
	//Character for name of the file
	char fileName[30];

	//CHARACTER USED TO READ EACH CHARACTER AT A TIME
	char c;

	//Reading file name into fileName
	printf("Please enter the file name: ");
	scanf("%s", fileName);

	//OPENING FILE TO CALCULATE NUMBER OF CHARACTER AND WORDS AND 
	//SMALLEST AND LONGEST NUMBER
	file = fopen(fileName, "r");

	//ERROR CHECKING IF FILE OPENED OR NOT
	if (file == NULL){ 
		printf("Error: File not read!!!!!!"); 
		scanf("%s", fileName);
	}

	//If file is opened it goes into this condition 
	else {
		//DECLARATIONS FOR ALL THE DIFFERENT TYPE OF RESULTS NEEDED
		int noWords = 0, noChar = 0, longWord = 0, shortWord = 100, a = 0, higher = 0, lower = 0;
		float average = 0;
		float higherPercentage = 0, lowerPercentage = 0;
	
		//DO WHILE TO CALCULATE NUMBER OF CHARACTER 
		//AND WORDS AND SMALLEST AND LONGEST NUMBER
		//Reading a character from file
		while( (c = getc(file)) != EOF)
		{
			//CHECKING TO SEE IF THE CHARACTER THAT IS READ IS A ALPHA CHARACTER
			if ((c >= 'A') && (c <= 'z'))
			{	a++; //counter for word length
				noChar++; // counter for no. of words
			}
			
			//IF IT IS NOT AN ALPHACHARACTER AND IF WORD HASN'T ALREADY 
			//BEEN RESET RUN THIS LOOP SO SAY WE
			//HAVE SOMETHING THATS ;FOLLOWED BY A SPACE IT 
			//WOULDN'T INCREASE THE NUMBER OF WORDS THEN OR 
			//EFFECT THE SHORT WORD TO BE 1.
			else if(a > 0){

				if(a < shortWord)// to determine shortest word
					shortWord = a;
				
				if(a > longWord) // to determine longest word
					longWord = a;
				
				noWords++; // counting no of words
				a = 0; // resetting length of word 
			}
		
		}
		
		if(a > 0)
			noWords++;
		
		//CALCULATE AVERAGE BY DIVIDING NUMBER OF CHARACTERS AND 
		//NO OF WORDS
		average = (float)noChar /(float)noWords;
		
		//CLOSING FILE FOR THE FIRST LOOP USED TO CALCULATE NUMBER OF 
		//CHARACTER AND WORDS AND SMALLEST AND LONGEST NUMBER
		fclose(file);
		
		//FILE OPENED TO CALCULATE HOW MANY WORDS ARE HIGHER 
		//THEN AVERAGE AND LOWER THEN AVERAGE
		file = fopen(fileName, "r");
		
		//LOOP USED TO CALCULATE HOW MANY WORDS ARE HIGHER 
		//THEN AVERAGE AND LOWER THEN AVERAGE
		//Reading each character from file for second loop		
		while((c = getc(file))!= EOF)
		{

			//Checking if alpha numeric or not
			if ((c >= 'A') && (c <= 'z'))
			{	
				//couter for word length
				a++;
			}
			
			// Doing same as the first loop
			else if(a > 0){
				if (a > average) // checking if word is bigger then average 
					higher++; // counter for how many words bigger then average
				if (a < average) // checking if word is smaller then average
					lower++; // counter for how many words smaller then average
				a = 0; // resetting character count for each word
			}
		
		}

		if(a > 0){
				if (a > average) // checking if word is bigger then average 
					higher++; // counter for how many words bigger then average
				if (a < average) // checking if word is smaller then average
					lower++; // counter for how many words smaller then average
		}

		//CALCULATING HIGHER AND LOWER PERCENTAGES for how many words are longer
		//and smaller then average
		higherPercentage = ((float)higher/(float)noWords) * 100;
		lowerPercentage  = ((float)lower/ (float)noWords) * 100;
		
		//CLOSE FILE OPENED FOR SECOND LOOP
		fclose(file);

		//PRINTS THE OUTPUT
		printf("The number of words in file: %d\n", noWords);
		printf("Longest word length is: %d\n", longWord);
		printf("Shortest word length is: %d\n", shortWord);
		printf("Average word length is %3.2f\n", average);
		printf("Percentage of words more than avg: %3.2f\n", higherPercentage);
		printf("Percentage of words less than avg: %3.2f\n", lowerPercentage);
		scanf("%s", fileName);
	}
	}